package org.pine.dictionary.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 整数code基础枚举接口
 * </p>
 *
 * @author pine
 * @since 2024-05-16
 */
public interface IntegerCodeBaseEnum extends BaseEnum<Integer> {

    static <T extends IntegerCodeBaseEnum> T convertToEnum(Integer code, Class<T> clazz) {
        if (code == null) {
            return null;
        } else {
            List<T> values = IntegerCodeBaseEnum.getEnumList(clazz);
            return values.stream()
                    .filter(item -> Objects.equals(item.getCode(), code))
                    .findAny()
                    .orElse(null);
        }
    }

    static <T extends IntegerCodeBaseEnum> T convertToEnum(String desc, Class<T> cls) {
        if (desc == null) {
            return null;
        } else {
            List<T> values = IntegerCodeBaseEnum.getEnumList(cls);
            return values.stream()
                    .filter((item) -> Objects.equals(item.getDesc(), desc))
                    .findAny()
                    .orElse(null);
        }
    }

    static <T extends IntegerCodeBaseEnum> List<T> getEnumList(Class<T> enumClass) {
        return new ArrayList<T>(Arrays.asList(enumClass.getEnumConstants()));
    }
}
